package com.atcjx.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName portalCorsfilter.java
 * @Description 远程访问
 * @createTime 2021年09月18日 22:58:00
 */
@Configuration
public class portalCorsfilter extends CorsFilter {
    private static Logger logger = LoggerFactory.getLogger(portalCorsfilter.class);
    public portalCorsfilter() {
        super(configurationSource());
    }

    public portalCorsfilter(CorsConfigurationSource configSource) {
        super(configSource);
    }

    private static UrlBasedCorsConfigurationSource configurationSource() {
        logger.info("init CorsFilter...");
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        //允许源，允许所有源的访问
        config.addAllowedOrigin("*");
        //允许所有的请求头
        config.addAllowedHeader("*");
        config.setMaxAge(36000L);
        config.setAllowedMethods(Arrays.asList("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return source;
    }
}
